/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: May 23, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.hce.clustering.normalize;

import edu.psu.geovista.data.model.AppDataModel;
import edu.psu.geovista.data.model.DefaultAppDataModel;

import java.util.Arrays;
import java.util.Vector;

public class Normalizer {

    public static AppDataModel normalize(DefaultAppDataModel datamodel){
        DefaultAppDataModel nmlModel=new DefaultAppDataModel();

        int numOfClumn = datamodel.getColumnCount();// .getNumDimensions();
        for (int i=0;i<numOfClumn;i++){
            float[] acolumn = datamodel.getColumnValueAsFloat(i);
            float[] normaldata = rangeNormalize(acolumn);

            String columnName = datamodel.getColumnName(i);
            nmlModel.addColumn(columnName,normaldata);
        }
        nmlModel.setColumns(datamodel.getColumns());

        Vector rowNames = datamodel.getRowNames();
      
        nmlModel.setRowNames(rowNames);// setRowNames(rowNameColumn,rowNames);
        return nmlModel;
    }
     public static float[] rangeNormalize(Vector  data){
         float[] fdata = DefaultAppDataModel.convertVectorToFloat(data);
         float[] normalData = rangeNormalize(fdata);
         return normalData;

     }
    public static float[] rangeNormalize(float[] data){
        int size = data.length;
        float[] normaldata=new float[size];
        float[] copy=new float[size ];

        System.arraycopy(data,0,copy,0,size );

        Arrays.sort(copy);
        float min=copy[0];
        float max = copy[size-1];


        for (int i=0;i<size;i++){
            normaldata[i] = (float) rangeNormalize(min,max,data[i]);
        }
        return normaldata;
    }

    public static double rangeNormalize(double min,double max,double value){
        double v=0;
        if(max!=min){
            v=(value-min)/(max-min);
        }
        else{
            if(max==0){
                v=0;
            }
            else{
                v=1;
            }
        }
        return v;

    }
}
